using GameLogic.AbilitySystem.Attribute;
using GameLogic.AbilitySystem.Effect;
using System;
using System.Collections;
using System.Collections.Generic;
using GameLogic.AbilitySystem.Tags;
using UnityEngine;

namespace GameLogic.AbilitySystem.Ability
{
    /// <summary>
    /// 基本技能类
    /// </summary>
    [Serializable]
    [CreateAssetMenu(menuName = "数值系统/创建新的技能")]
    public class AbilityBase : ScriptableObject
    {
        /// <summary>
        /// 标签集合
        /// </summary>
        public HashSet<string> Tags;

        /// <summary>
        /// 有标签
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool ContainsTag(string tag)
        {
            return Tags.Contains(tag);
        }

        /// <summary>
        /// 技能ID`
        /// </summary>
        public int ID;

        /// <summary>
        /// 需求能力值，不进行消耗
        /// </summary>
        public AttributeSet RequirementAttributeSet;

        /// <summary>
        /// 消耗的能力值
        /// </summary>
        public AttributeSet CostAttributeSet;

        /// <summary>
        /// 造成的效果
        /// </summary>
        public EffectBase ResultEffect;


        /// <summary>
        /// 检查能力集是否满足需求和消耗
        /// 此时已经扣掉了cost，可以直接发动
        /// </summary>
        /// <returns></returns>
        public virtual bool CheckAbilityRequirementAndCost(CharacterAttributeSet characterAttributeSet)
        {
            return (CheckAbilityRequirement(characterAttributeSet) && CheckAbilityCost(characterAttributeSet));
        }

        /// <summary>
        /// 检查能力集能否满足消耗
        /// 仅限表现数值
        /// </summary>
        /// <param name="characterAttributeSet"></param>
        /// <returns></returns>
        public virtual bool CheckAbilityCost(CharacterAttributeSet characterAttributeSet)
        {
            if (characterAttributeSet.attributeSet <= CostAttributeSet)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 检查能力集能否满足要求
        /// </summary>
        /// <param name="characterAttributeSet"></param>
        /// <returns></returns>
        public virtual bool CheckAbilityRequirement(CharacterAttributeSet characterAttributeSet)
        {
            return characterAttributeSet.attributeSet >= RequirementAttributeSet;
        }

        /// <summary>
        /// 消耗
        /// </summary>
        /// <param name="characterAttributeSet"></param>
        /// <returns></returns>
        public virtual bool ApplyCost(CharacterAttributeSet characterAttributeSet)
        {
            if (characterAttributeSet.attributeSet <= CostAttributeSet)
            {
                return false;
            }

            characterAttributeSet.attributeSet.Modify(CostAttributeSet.ToAttributeModifiers(-1f));
            return true;
        }

        /// <summary>
        /// 造成effect
        /// </summary>
        /// <param name="self"></param>
        /// <param name="target"></param>
        public void ApplyEffect(AbilitySystemComponent self, AbilitySystemComponent target, float mul = 1.0f)
        {
            target.AttributeSet.EffectContainer.AddEffect(ResultEffect, mul);
            OnEffectApply();
        }


        /// <summary>
        /// 实行
        /// </summary>
        /// <param name="self"></param>
        public void Activate(AbilitySystemComponent self)
        {
            OnActivate();
        }

        /// <summary>
        /// 技能发动之后的逻辑
        /// </summary>
        public virtual void OnActivate()
        {

        }

        /// <summary>
        /// 施加Effect时的逻辑，如产生音效等
        /// </summary>
        public virtual void OnEffectApply()
        {

        }
    }
}
